﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Services;

using com.vs.DataAccess;
using com.vs.DataAccess.DO;
using com.vs.General;
using com.vs.General.BL;

namespace com.vs.WebService
{
    /// <summary>
    /// Summary description for Company
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Company : System.Web.Services.WebService
    {
        public static string XSLT_DIR = @"xslt";

        [WebMethod]
        public string GetCompanies(string stSessID, string stCondXML)
        {
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;
            //string stRsltXML = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dt = CompanyDO.GetAllToTable(sess);
                sbXML = new StringBuilder();
                dt.TableName = "COMPANIES";
                dt.DataSet.DataSetName = "COMPANY";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                //stRsltXML = sbXML.ToString();
                //stRsltXML = XMLDocManager.TransformStringWithFile(stRsltXML, "", Encoding.UTF8);
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }


        [WebMethod]
        public string GetWarehouses(string stSessID, string stCondXML)
        {
            List<KeyValuePair<string, KeyValuePair<string, object>>> dcCond = null;
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;
            //string stRsltXML = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                if (!StringManager.IsEmpty(stCondXML))
                    dcCond = XMLDocManager.BuildQueryCondition(stCondXML);

                dt = WarehouseDO.GetAllToTable(sess);
                sbXML = new StringBuilder();
                dt.TableName = "WAREHOUSE";
                dt.DataSet.DataSetName = "WAREHOUSES";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                //stRsltXML = sbXML.ToString();
                //stRsltXML = XMLDocManager.TransformStringWithFile(stRsltXML, "", Encoding.UTF8);
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }


        [WebMethod]
        public string GetStates(string stSessID)
        {
            return this.GetGenericConfigs(stSessID, "STATE");
        }

        [WebMethod]
        public string GetTaskTypes(string stSessID)
        {
            return this.GetGenericConfigs(stSessID, "TASK_TYPE");
        }

        [WebMethod]
        public string GetReceiptTypes(string stSessID)
        {
            return this.GetGenericConfigs(stSessID, "RECEIPT_TYPE");
        }

        [WebMethod]
        public string GetShipmentTypes(string stSessID)
        {
            return this.GetGenericConfigs(stSessID, "SHIPMENT_TYPE");
        }

        [WebMethod]
        public string GetInventorySts(string stSessID)
        {
            return this.GetGenericConfigs(stSessID, "INVENTORY_STS");
        }

        [WebMethod]
        public string GetLocationClasses(string stSessID)
        {
            return this.GetGenericConfigs(stSessID, "LOCATION_CLASS");
        }

        [WebMethod]
        public string GetLocationTypes(string stSessID)
        {
            return this.GetGenericConfigs(stSessID, "LOCATION_TYPE");
        }

        [WebMethod]
        public string GetPackingClass(string stSessID)
        {
            return this.GetGenericConfigs(stSessID, "PACKING_CLASS");
        }

        [WebMethod]
        public string GetItemClass(string stSessID)
        {
            return this.GetGenericConfigs(stSessID, "ITEM_CLASS");
        }

        [WebMethod]
        public string GetQuantityUMs(string stSessID)
        {
            return this.GetGenericConfigs(stSessID, "QUANTITY_UM");
        }

        [WebMethod]
        public string GetDimensionUMs(string stSessID)
        {
            return this.GetGenericConfigs(stSessID, "DIMENSION_UM");
        }

        [WebMethod]
        public string GetWeightUMs(string stSessID)
        {
            return this.GetGenericConfigs(stSessID, "WEIGHT_UM");
        }

        [WebMethod]
        public string GetVolumeUMs(string stSessID)
        {
            return this.GetGenericConfigs(stSessID, "VOLUME_UM");
        }

        [WebMethod]
        public string GetGenericConfigs(string stSessID, string stRecordType)
        {
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;
            //string stTransXML = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                if (StringManager.IsEmpty(stRecordType))
                    return null;

                dt = GenericConfigDetailDO.GetTableByColumn(sess, "RECORD_TYPE", stRecordType);
                sbXML = new StringBuilder();
                dt.TableName = "GENERIC_CONFIG";
                dt.DataSet.DataSetName = "GENERIC_CONFIGS";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                //stRsltXML = sbXML.ToString();
                //stTransXML = XMLDocManager.TransformStringWithFile(sbXML.ToString(), XSLT_DIR + "GenericConfig.xslt", Encoding.UTF8);
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string GetAllocationRules(string stSessID)
        {
            //Dictionary<string, string> dcCond = null;
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;
            //string stRsltXML = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dt = AllocationRuleHeaderDO.GetTableByColumn(sess, null, null);
                sbXML = new StringBuilder();
                dt.TableName = "ALLOCATION_RULE_HEADER";
                dt.DataSet.DataSetName = "ALLOCATION_RULE_HEADERS";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                //stRsltXML = sbXML.ToString();
                //stRsltXML = XMLDocManager.TransformStringWithFile(stRsltXML, "", Encoding.UTF8);
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string GetLocatingRules(string stSessID)
        {
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;
            //string stRsltXML = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dt = LocatingRuleHeaderDO.GetTableByColumn(sess, null, null);
                sbXML = new StringBuilder();
                dt.TableName = "LOCATING_RULE_HEADER";
                dt.DataSet.DataSetName = "LOCATING_RULE_HEADERS";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                //stRsltXML = sbXML.ToString();
                //stRsltXML = XMLDocManager.TransformStringWithFile(stRsltXML, "", Encoding.UTF8);
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string GetDefaultWarehouse(string stSessID, string stUserID)
        {
            Session sess = null;
            UserDO usrDO = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                usrDO = UserDO.GetModelByPK(sess, stUserID);
                if (null == usrDO)
                {
                    return MessageManager.MSG_USR_002;//错误：用户不存在
                }
                return usrDO.DEFAULT_WHS;
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string GetWaveFlows(string stSessID)
        {
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;
            //string stRsltXML = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                dt = WaveFlowHeaderDO.GetTableByColumn(sess, null, null);
                sbXML = new StringBuilder();
                dt.TableName = "WAVE_FLOW_HEADER";
                dt.DataSet.DataSetName = "WAVE_FLOW_HEADERS";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                //stRsltXML = sbXML.ToString();
                //stRsltXML = XMLDocManager.TransformStringWithFile(stRsltXML, "", Encoding.UTF8);
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string GetCarriers(string stSessID)
        {
            return this.GetGenericConfigs(stSessID, "CARRIER");
        }

        [WebMethod]
        public string GetZones(string stSessID)
        {
            return this.GetGenericConfigs(stSessID, "ZONE");
        }

        [WebMethod]
        public string GetWaveSteps(string stSessID)
        {
            return this.GetGenericConfigs(stSessID, "WAVE_STEP");
        }

        public string GetItemCategories(string stSessID, int iIndex)
        {
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;
            string stRecordType = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;

                if ((iIndex < 1) || (iIndex > 8))
                    return MessageManager.MSG_ITM_004;

                stRecordType = "ITEM_CATEGORY" + iIndex;

                dt = GenericConfigDetailDO.GetTableByColumn(sess, "RECORD_TYPE", stRecordType);
                if (DataManager.IsEmpty(dt))
                    return null;
                sbXML = new StringBuilder();
                dt.TableName = "ITEM_CATEGORY" + iIndex;
                dt.DataSet.DataSetName = "ITEM_CATEGORY" + iIndex + "S";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                //return XMLDocManager.TransformStringWithFile(sbXML.ToString(), XSLT_DIR + "ItemLookUp.xslt", Encoding.UTF8);
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string GetItemCategory1(string stSessID)
        {
            return this.GetItemCategories(stSessID, 1);
        }

        [WebMethod]
        public string GetItemCategory2(string stSessID)
        {
            return this.GetItemCategories(stSessID, 2);
        }

        [WebMethod]
        public string GetItemCategory3(string stSessID)
        {
            return this.GetItemCategories(stSessID, 3);
        }

        [WebMethod]
        public string GetItemCategory4(string stSessID)
        {
            return this.GetItemCategories(stSessID, 4);
        }

        [WebMethod]
        public string GetItemCategory5(string stSessID)
        {
            return this.GetItemCategories(stSessID, 5);
        }

        [WebMethod]
        public string GetItemCategory6(string stSessID)
        {
            return this.GetItemCategories(stSessID, 6);
        }

        [WebMethod]
        public string GetItemCategory7(string stSessID)
        {
            return this.GetItemCategories(stSessID, 7);
        }

        [WebMethod]
        public string GetItemCategory8(string stSessID)
        {
            return this.GetItemCategories(stSessID, 8);
        }

        [WebMethod]
        public string GetAuditLogs(string stSessID, string stCondXML)
        {
            DataTable dt = null;
            Session sess = null;
            StringBuilder sbXML = null;
            List<KeyValuePair<string, KeyValuePair<string, object>>> dcCond = null;

            try
            {
                sess = (Session)Application.Get(stSessID);
                if (null == sess)
                    return MessageManager.MSG_SES_001;
                
                dcCond = new List<KeyValuePair<string, KeyValuePair<string, object>>>();
                if (!StringManager.IsEmpty(stCondXML))
                    dcCond = XMLDocManager.BuildQueryCondition(stCondXML);

                dt = AuditLogDO.GetTableByColumns(sess, dcCond, " ORDER BY INTERNAL_ID DESC ");;
                sbXML = new StringBuilder();
                dt.TableName = "AUDIT_LOG";
                dt.DataSet.DataSetName = "AUDIT_LOGS";
                dt.DataSet.WriteXml(new StringWriter(sbXML));
                //stRsltXML = sbXML.ToString();
                //stRsltXML = XMLDocManager.TransformStringWithFile(stRsltXML, "", Encoding.UTF8);
                return sbXML.ToString();
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
            finally
            {
            }
        }

        [WebMethod]
        public string GetInboundSts(string stSessID)
        {
            return this.GetGenericConfigs(stSessID, "INBOUND_STS");
        }

        [WebMethod]
        public string GetOutboundSts(string stSessID)
        {
            return this.GetGenericConfigs(stSessID, "OUTBOUND_STS");
        }

        [WebMethod]
        public string ValidateLicense()
        {
            LicenseKeyValidation licxMgr = null;

            try
            {
                licxMgr = new LicenseKeyValidation();
                //return licxMgr.ValidateLicense(null);
                return null;
            }
            catch (Exception e)
            {
                return MessageManager.MSG_GEN_000 + e.Message;
            }
        }

        [WebMethod]
        public string GetContainerType(string stSessID)
        {
            return this.GetGenericConfigs(stSessID, "CONTAINER_TYPE");
        }
    }
}
